package edu.hnzj.service;

import java.util.List;

import javax.annotation.Resource;

import org.junit.runner.Result;
import org.springframework.stereotype.Service;

import edu.hnzj.dao.CommodityDao;
import edu.hnzj.entity.Commodity;
import edu.hnzj.util.JsonResult;
import jdk.nashorn.internal.runtime.linker.LinkerCallSite;

@Service("commodityService")
public class CommodityServiceImp  implements CommodityService{
	
    @Resource
    private CommodityDao dao;
	public JsonResult<List<Commodity>> commodityList() {
		JsonResult<List<Commodity>> result=new JsonResult<List<Commodity>>();
		 List<Commodity> list= dao.findAll();
		if (list.size() == 0) {
			result.setStatus(1);
			result.setMsg("没有查询到任何员工信息");
			result.setData(list);
			return result;
		}
		
		result.setStatus(0);
		result.setData(list);
		return result;
		
	}
	public JsonResult<Object> add(Commodity commodity) {
		JsonResult<Object> result =new JsonResult<Object>();
		
		Commodity com = new Commodity();
		com.setSname(commodity.getSname());
		com.setSnum(commodity.getSnum());
		com.setSprice(commodity.getSprice());
		com.setSleib(commodity.getSleib());
		dao.save(com);

		result.setStatus(0);
		result.setMsg("添加成功");
		return result;
		
	}
	public JsonResult<Object> delete(int id) {
		JsonResult<Object> result =new JsonResult<Object>();
		int delete = dao.delete(id);
		if (delete == 0) {
			result.setStatus(1);
			result.setMsg("删除失败");
			return result;
		}else {
			result.setStatus(0);
			result.setMsg("删除成功");
			return result;
		}
	}
	public JsonResult<Object> update(Commodity commodity) {
		JsonResult<Object> result =new JsonResult<Object>();

		Commodity com = new Commodity();
		com.setSid(commodity.getSid());
		com.setSname(commodity.getSname());
		com.setSnum(commodity.getSnum());
		com.setSprice(commodity.getSprice());
		com.setSleib(commodity.getSleib());
	    dao.update(com);
		return result;
	}
	
	public JsonResult<List<Commodity>> findById(int id) {
		JsonResult<List<Commodity>> result =new JsonResult<List<Commodity>>();
		List<Commodity> list = dao.findById(id);
		if(list.size() == 0) {
			result.setStatus(1);
			result.setMsg("查询失败");
			return result;
		}
		result.setStatus(0);
		result.setMsg("查询成功");
		result.setData(list);
		return result;
	}
	

}
